<!DOCTYPE html>
<html lang="zh-Hans">
<head>

    <!--[if lt IE 9]>
        <style>body {display: none; background: none !important} </style>
        <meta http-equiv="Refresh" Content="0; url=//outdatedbrowser.com/" />
    <![endif]-->

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<meta name="author" content="HatBoy" />



<meta name="description" content="使用子进程">
<meta name="keywords" content="Python,asyncio,异步并发">
<meta property="og:type" content="article">
<meta property="og:title" content="asyncio之使用子进程">
<meta property="og:url" content="http://hatboy.gitee.io/2017/12/21/asyncio之使用子进程/index.html">
<meta property="og:site_name" content="HatBoy的个人主页">
<meta property="og:description" content="使用子进程">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-12-21T10:43:18.763Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="asyncio之使用子进程">
<meta name="twitter:description" content="使用子进程">

<link rel="apple-touch-icon" href= "/apple-touch-icon.png">


    <link rel="alternate" href="/atom.xml" title="HatBoy的个人主页" type="application/atom+xml">



    <link rel="shortcut icon" href="/img/favicon.ico">



    <link href="//cdn.bootcss.com/animate.css/3.5.1/animate.min.css" rel="stylesheet">



    <link href="//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet">



    <script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
    <link href="//cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-minimal.css" rel="stylesheet">


<link rel="stylesheet" href="/css/style.css">



<link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">


<title>asyncio之使用子进程 | HatBoy的个人主页</title>

<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

<script>
    var yiliaConfig = {
        fancybox: true,
        animate: true,
        isHome: false,
        isPost: true,
        isArchive: false,
        isTag: false,
        isCategory: false,
        fancybox_js: "//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.js",
        scrollreveal: "//cdn.bootcss.com/scrollReveal.js/3.1.4/scrollreveal.min.js",
        search: true
    }
</script>


    <script>
        yiliaConfig.jquery_ui = [true, "//cdn.bootcss.com/jqueryui/1.10.4/jquery-ui.min.js", "//cdn.bootcss.com/jqueryui/1.10.4/css/jquery-ui.min.css"];
    </script>



    <script> yiliaConfig.rootUrl = "\/";</script>





    <script>
        var _hmt = _hmt || [];
        (function() {
            var hm = document.createElement("script");
            hm.src = "//hm.baidu.com/hm.js?04d04bb57e5c5eaf49059aa602440263";
            var s = document.getElementsByTagName("script")[0]; 
            s.parentNode.insertBefore(hm, s);
        })();
    </script>


</head>
<body>
  <div id="container">
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
    <header id="header" class="inner">
        <a href="/" class="profilepic">
            <img src="/img/avatar.jpg" class="animated zoomIn">
        </a>
        <hgroup>
          <h1 class="header-author"><a href="/">HatBoy</a></h1>
        </hgroup>

        

        
            <form id="search-form">
            <input type="text" id="local-search-input" name="q" placeholder="search..." class="search form-control" autocomplete="off" autocorrect="off" searchonload="true" />
            <i class="fa fa-times" onclick="resetSearch()"></i>
            </form>
            <div id="local-search-result"></div>
            <p class='no-result'>No results found <i class='fa fa-spinner fa-pulse'></i></p>
        


        
            <div id="switch-btn" class="switch-btn">
                <div class="icon">
                    <div class="icon-ctn">
                        <div class="icon-wrap icon-house" data-idx="0">
                            <div class="birdhouse"></div>
                            <div class="birdhouse_holes"></div>
                        </div>
                        <div class="icon-wrap icon-ribbon hide" data-idx="1">
                            <div class="ribbon"></div>
                        </div>
                        
                        <div class="icon-wrap icon-link hide" data-idx="2">
                            <div class="loopback_l"></div>
                            <div class="loopback_r"></div>
                        </div>
                        
                        
                        <div class="icon-wrap icon-me hide" data-idx="3">
                            <div class="user"></div>
                            <div class="shoulder"></div>
                        </div>
                        
                    </div>
                    
                </div>
                <div class="tips-box hide">
                    <div class="tips-arrow"></div>
                    <ul class="tips-inner">
                        <li>菜单</li>
                        <li>标签</li>
                        
                        <li>友情链接</li>
                        
                        
                        <li>关于我</li>
                        
                    </ul>
                </div>
            </div>
        

        <div id="switch-area" class="switch-area">
            <div class="switch-wrap">
                <section class="switch-part switch-part1">
                    <nav class="header-menu">
                        <ul>
                        
                            <li><a href="/">主页</a></li>
                        
                            <li><a href="/archives/">所有文章</a></li>
                        
                            <li><a href="/tags/">标签云</a></li>
                        
                            <li><a href="/about/">关于我</a></li>
                        
                        </ul>
                    </nav>
                    <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" href="mailto:hatboy-dj@qq.com" title="Email"></a>
                            
                                <a class="fa GitHub" href="https://github.com/HatBoy/" title="GitHub"></a>
                            
                                <a class="fa GitHub博客" href="https://hatboy.github.io/" title="GitHub博客"></a>
                            
                                <a class="fa CSDN" href="http://blog.csdn.net/dj1174232716" title="CSDN"></a>
                            
                        </ul>
                    </nav>
                </section>
                
                
                <section class="switch-part switch-part2">
                    <div class="widget tagcloud" id="js-tagcloud">
                        <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Cython/">Cython</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Python/">Python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Python-tips/">Python tips</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Python模块tips/">Python模块tips</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Python语法/">Python语法</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/asyncio/">asyncio</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/异步并发/">异步并发</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/调试/">调试</a></li></ul>
                    </div>
                </section>
                
                
                
                <section class="switch-part switch-part3">
                    <div id="js-friends">
                    
                      <a class="main-nav-link switch-friends-link" href="http://fmxfmx.gitee.io/">FMX</a>
                    
                      <a class="main-nav-link switch-friends-link" href="http://jewelz.me/">Jewelz</a>
                    
                      <a class="main-nav-link switch-friends-link" href="https://www.zhihu.com/people/talentisan/activities">轻狂书生</a>
                    
                      <a class="main-nav-link switch-friends-link" href="http://threetop.top/">ZCC</a>
                    
                      <a class="main-nav-link switch-friends-link" href="http://blog.csdn.net/u012017783">ZCC的CSDN</a>
                    
                      <a class="main-nav-link switch-friends-link" href="http://zcc888.gitee.io/">ZCC的博客</a>
                    
                    </div>
                </section>
                

                
                
                <section class="switch-part switch-part4">
                
                    <div id="js-aboutme">一枚爱Python,爱海贼,爱健身,喜欢好玩有趣事物的程序员……</div>
                </section>
                
            </div>
        </div>
    </header>                
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
      <div class="overlay">
          <div class="slider-trigger"></div>
          <h1 class="header-author js-mobile-header hide"><a href="/" title="回到主页">HatBoy</a></h1>
      </div>
    <div class="intrude-less">
        <header id="header" class="inner">
            <a href="/" class="profilepic">
                <img src="/img/avatar.jpg" class="animated zoomIn">
            </a>
            <hgroup>
              <h1 class="header-author"><a href="/" title="回到主页">HatBoy</a></h1>
            </hgroup>
            
            <nav class="header-menu">
                <ul>
                
                    <li><a href="/">主页</a></li>
                
                    <li><a href="/archives/">所有文章</a></li>
                
                    <li><a href="/tags/">标签云</a></li>
                
                    <li><a href="/about/">关于我</a></li>
                
                <div class="clearfix"></div>
                </ul>
            </nav>
            <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" target="_blank" href="mailto:hatboy-dj@qq.com" title="Email"></a>
                            
                                <a class="fa GitHub" target="_blank" href="https://github.com/HatBoy/" title="GitHub"></a>
                            
                                <a class="fa GitHub博客" target="_blank" href="https://hatboy.github.io/" title="GitHub博客"></a>
                            
                                <a class="fa CSDN" target="_blank" href="http://blog.csdn.net/dj1174232716" title="CSDN"></a>
                            
                        </ul>
            </nav>
        </header>                
    </div>
    <link class="menu-list" tags="标签" friends="友情链接" about="关于我"/>
</nav>
      <div class="body-wrap"><article id="post-asyncio之使用子进程" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/2017/12/21/asyncio之使用子进程/" class="article-date">
      <time datetime="2017-12-21T10:41:49.000Z" itemprop="datePublished">2017-12-21</time>
</a>


    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      asyncio之使用子进程
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        

        
    <div class="article-tag tagcloud">
        <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Python/">Python</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/asyncio/">asyncio</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/异步并发/">异步并发</a></li></ul>
    </div>

        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
          
        <excerpt in="" index="" |="" 首页摘要="">

<p>使用子进程</p>
<a id="more"></a>
<the rest="" of="" contents="" |="" 余下全文="">

<p>和其他程序或者进程一起工作的需求很常见，利用现有的代码而不用重写它，或者访问Python中没有的库或功能。与网络I/O一样，asyncio包含两个抽象，用于启动另一个程序，然后与之交互。</p>
<h3 id="使用协议抽象子进程"><a href="#使用协议抽象子进程" class="headerlink" title="使用协议抽象子进程"></a>使用协议抽象子进程</h3><p>这个例子使用一个协程启动一个进程来运行Unix命令df来查找本地磁盘上的可用空间。它使用subprocess_exec()启动进程，并将其绑定到一个协议类，该类知道如何读取df命令输出并解析它。协议类的方法根据子进程的I/O事件自动调用。由于stdin和stderr参数都设置为None，因此这些通信通道未连接到新进程。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> asyncio</div><div class="line"><span class="keyword">import</span> functools</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">def</span> <span class="title">run_df</span><span class="params">(loop)</span>:</span></div><div class="line">    print(<span class="string">'in run_df'</span>)</div><div class="line"></div><div class="line">    cmd_done = asyncio.Future(loop=loop)</div><div class="line">    factory = functools.partial(DFProtocol, cmd_done)</div><div class="line">    proc = loop.subprocess_exec(</div><div class="line">        factory,</div><div class="line">        <span class="string">'df'</span>, <span class="string">'-hl'</span>,</div><div class="line">        stdin=<span class="keyword">None</span>,</div><div class="line">        stderr=<span class="keyword">None</span>,</div><div class="line">    )</div><div class="line">    <span class="keyword">try</span>:</div><div class="line">        print(<span class="string">'launching process'</span>)</div><div class="line">        transport, protocol = <span class="keyword">await</span> proc</div><div class="line">        print(<span class="string">'waiting for process to complete'</span>)</div><div class="line">        <span class="keyword">await</span> cmd_done</div><div class="line">    <span class="keyword">finally</span>:</div><div class="line">        transport.close()</div><div class="line"></div><div class="line">    <span class="keyword">return</span> cmd_done.result()</div></pre></td></tr></table></figure></p>
<p>DFProtocol类是SubprocessProtocol的子类，它定义了一个通过管道与另一个进程通信的API。done参数将成为调用者用来监视进程完成的Future。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">DFProtocol</span><span class="params">(asyncio.SubprocessProtocol)</span>:</span></div><div class="line"></div><div class="line">    FD_NAMES = [<span class="string">'stdin'</span>, <span class="string">'stdout'</span>, <span class="string">'stderr'</span>]</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self, done_future)</span>:</span></div><div class="line">        self.done = done_future</div><div class="line">        self.buffer = bytearray()</div><div class="line">        super().__init__()</div></pre></td></tr></table></figure></p>
<p>与socket通信一样，当设置新进程的输入通道时，会调用connection_made()。transport参数是BaseSubprocessTransport子类的一个实例。如果进程配置为接收输入，它可以读取进程输出的数据并将数据写入进程的输入流。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">connection_made</span><span class="params">(self, transport)</span>:</span></div><div class="line">    print(<span class="string">'process started &#123;&#125;'</span>.format(transport.get_pid()))</div><div class="line">    self.transport = transport</div></pre></td></tr></table></figure></p>
<p>当进程产生输出时，pipe_data_received()被调用，其中文件描述符是来自数据发送的文件，实际的数据是从管道中读取的。协议类将进程的标准输出通道的输出保存在缓冲区中供以后处理。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">pipe_data_received</span><span class="params">(self, fd, data)</span>:</span></div><div class="line">    print(<span class="string">'read &#123;&#125; bytes from &#123;&#125;'</span>.format(len(data), self.FD_NAMES[fd]))</div><div class="line">    <span class="keyword">if</span> fd == <span class="number">1</span>:</div><div class="line">        self.buffer.extend(data)</div></pre></td></tr></table></figure></p>
<p>当进程终止时，process_exited()被调用。通过调用get_returncode()可以从传输对象获得进程的退出代码。在这种情况下，如果没有错误报告，则在通过Future实例返回之前，可用的输出将被解码和解析。如果有错误，则结果被假设为空。设置将来的结果告诉run_df()该进程已经退出，所以它将清理并返回结果。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">process_exited</span><span class="params">(self)</span>:</span></div><div class="line">    print(<span class="string">'process exited'</span>)</div><div class="line">    return_code = self.transport.get_returncode()</div><div class="line">    print(<span class="string">'return code &#123;&#125;'</span>.format(return_code))</div><div class="line">    <span class="keyword">if</span> <span class="keyword">not</span> return_code:</div><div class="line">        cmd_output = bytes(self.buffer).decode()</div><div class="line">        results = self._parse_results(cmd_output)</div><div class="line">    <span class="keyword">else</span>:</div><div class="line">        results = []</div><div class="line">    self.done.set_result((return_code, results))</div></pre></td></tr></table></figure></p>
<p>命令输出被解析成一系列字典，将每个输出行的标题名称映射到它们的值，并返回结果列表。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">_parse_results</span><span class="params">(self, output)</span>:</span></div><div class="line">    print(<span class="string">'parsing results'</span>)</div><div class="line">    <span class="keyword">if</span> <span class="keyword">not</span> output:</div><div class="line">        <span class="keyword">return</span> []</div><div class="line">    lines = output.splitlines()</div><div class="line">    headers = lines[<span class="number">0</span>].split()</div><div class="line">    devices = lines[<span class="number">1</span>:]</div><div class="line">    results = [</div><div class="line">        dict(zip(headers, line.split()))</div><div class="line">        <span class="keyword">for</span> line <span class="keyword">in</span> devices</div><div class="line">    ]</div><div class="line">    <span class="keyword">return</span> results</div></pre></td></tr></table></figure></p>
<p>run_df()协程使用run_until_complete()运行，然后检查结果并打印每个设备上的可用空间。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">event_loop = asyncio.get_event_loop()</div><div class="line"><span class="keyword">try</span>:</div><div class="line">    return_code, results = event_loop.run_until_complete(</div><div class="line">        run_df(event_loop)</div><div class="line">    )</div><div class="line"><span class="keyword">finally</span>:</div><div class="line">    event_loop.close()</div><div class="line"></div><div class="line"><span class="keyword">if</span> return_code:</div><div class="line">    print(<span class="string">'error exit &#123;&#125;'</span>.format(return_code))</div><div class="line"><span class="keyword">else</span>:</div><div class="line">    print(<span class="string">'\nFree space:'</span>)</div><div class="line">    <span class="keyword">for</span> r <span class="keyword">in</span> results:</div><div class="line">        print(<span class="string">'&#123;Mounted:25&#125;: &#123;Avail&#125;'</span>.format(**r))</div></pre></td></tr></table></figure></p>
<p>下面的输出显示了所用步骤的顺序，以及运行系统的三个驱动器上的可用空间。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">$ python3 asyncio_subprocess_protocol.py</div><div class="line"></div><div class="line"><span class="keyword">in</span> run_df</div><div class="line">launching process</div><div class="line">process started <span class="number">49675</span></div><div class="line">waiting <span class="keyword">for</span> process to complete</div><div class="line">read <span class="number">332</span> bytes <span class="keyword">from</span> stdout</div><div class="line">process exited</div><div class="line"><span class="keyword">return</span> code <span class="number">0</span></div><div class="line">parsing results</div><div class="line"></div><div class="line">Free space:</div><div class="line">/                        : <span class="number">233</span>Gi</div><div class="line">/Volumes/hubertinternal  : <span class="number">157</span>Gi</div><div class="line">/Volumes/hubert-tm      : <span class="number">2.3</span>Ti</div></pre></td></tr></table></figure></p>
<h3 id="使用协程和流子进程"><a href="#使用协程和流子进程" class="headerlink" title="使用协程和流子进程"></a>使用协程和流子进程</h3><p>要使用协程直接运行进程，而不是通过协议子类访问它，调用create_subprocess_exec()并指定哪个stdout，stderr和stdin连接到管道。协程生成子进程的结果是一个Process实例，可用于操纵子进程或与之进行通信。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> asyncio</div><div class="line"><span class="keyword">import</span> asyncio.subprocess</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">def</span> <span class="title">run_df</span><span class="params">()</span>:</span></div><div class="line">    print(<span class="string">'in run_df'</span>)</div><div class="line"></div><div class="line">    buffer = bytearray()</div><div class="line"></div><div class="line">    create = asyncio.create_subprocess_exec(</div><div class="line">        <span class="string">'df'</span>, <span class="string">'-hl'</span>,</div><div class="line">        stdout=asyncio.subprocess.PIPE,</div><div class="line">    )</div><div class="line">    print(<span class="string">'launching process'</span>)</div><div class="line">    proc = <span class="keyword">await</span> create</div><div class="line">    print(<span class="string">'process started &#123;&#125;'</span>.format(proc.pid))</div></pre></td></tr></table></figure></p>
<p>在这个例子中，除了命令行参数外，df不需要任何输入，所以下一步就是读取所有的输出。有了Protocol，就无法控制一次读取多少数据。这个例子使用readline()，但是它也可以直接调用read()来读取不是面向行的数据。与协议示例一样，该命令的输出被缓冲，因此可以稍后进行解析。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">while</span> <span class="keyword">True</span>:</div><div class="line">    line = <span class="keyword">await</span> proc.stdout.readline()</div><div class="line">    print(<span class="string">'read &#123;!r&#125;'</span>.format(line))</div><div class="line">    <span class="keyword">if</span> <span class="keyword">not</span> line:</div><div class="line">        print(<span class="string">'no more output from command'</span>)</div><div class="line">        <span class="keyword">break</span></div><div class="line">    buffer.extend(line)</div></pre></td></tr></table></figure></p>
<p>当程序运行完成没有更多的输出时，readline()方法返回一个空的字节字符串。为确保正确清理进程，下一步是等待进程完全退出。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">print(<span class="string">'waiting for process to complete'</span>)</div><div class="line"><span class="keyword">await</span> proc.wait()</div></pre></td></tr></table></figure></p>
<p>此时，可以检查退出状态，以确定是解析输出还是处理错误，因为它不产生输出。解析逻辑和前面的例子是一样的，但是是独立的函数（这里没有显示），因为没有协议类来隐藏它。在解析数据之后，返回结果和退出代码给来调用者。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">return_code = proc.returncode</div><div class="line">print(<span class="string">'return code &#123;&#125;'</span>.format(return_code))</div><div class="line"><span class="keyword">if</span> <span class="keyword">not</span> return_code:</div><div class="line">    cmd_output = bytes(buffer).decode()</div><div class="line">    results = _parse_results(cmd_output)</div><div class="line"><span class="keyword">else</span>:</div><div class="line">    results = []</div><div class="line"></div><div class="line"><span class="keyword">return</span> (return_code, results)</div></pre></td></tr></table></figure></p>
<p>主程序与基于协议的示例类似，因为实现更改在run_df()中是独立的。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">event_loop = asyncio.get_event_loop()</div><div class="line"><span class="keyword">try</span>:</div><div class="line">    return_code, results = event_loop.run_until_complete(</div><div class="line">        run_df()</div><div class="line">    )</div><div class="line"><span class="keyword">finally</span>:</div><div class="line">    event_loop.close()</div><div class="line"></div><div class="line"><span class="keyword">if</span> return_code:</div><div class="line">    print(<span class="string">'error exit &#123;&#125;'</span>.format(return_code))</div><div class="line"><span class="keyword">else</span>:</div><div class="line">    print(<span class="string">'\nFree space:'</span>)</div><div class="line">    <span class="keyword">for</span> r <span class="keyword">in</span> results:</div><div class="line">        print(<span class="string">'&#123;Mounted:25&#125;: &#123;Avail&#125;'</span>.format(**r))</div></pre></td></tr></table></figure></p>
<p>由于df的输出可以一次读取一行，因此可以显示程序的进度。否则，输出看起来类似于前面的例子。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line">$ python3 asyncio_subprocess_coroutine.py</div><div class="line"></div><div class="line"><span class="keyword">in</span> run_df</div><div class="line">launching process</div><div class="line">process started <span class="number">49678</span></div><div class="line">read <span class="string">b'Filesystem    Size  Used  Avail Capacity  iused</span></div><div class="line"><span class="string">ifree %iused  Mounted on\n'</span></div><div class="line">read <span class="string">b'/dev/disk2s2  446Gi  213Gi  233Gi    48%  55955082</span></div><div class="line"><span class="string">61015132  48%  /\n'</span></div><div class="line">read <span class="string">b'/dev/disk1    465Gi  307Gi  157Gi    67%  80514922</span></div><div class="line"><span class="string">41281172  66%  /Volumes/hubertinternal\n'</span></div><div class="line">read <span class="string">b'/dev/disk3s2  3.6Ti  1.4Ti  2.3Ti    38% 181837749</span></div><div class="line"><span class="string">306480579  37%  /Volumes/hubert-tm\n'</span></div><div class="line">read <span class="string">b''</span></div><div class="line">no more output <span class="keyword">from</span> command</div><div class="line">waiting <span class="keyword">for</span> process to complete</div><div class="line"><span class="keyword">return</span> code <span class="number">0</span></div><div class="line">parsing results</div><div class="line"></div><div class="line">Free space:</div><div class="line">/                        : <span class="number">233</span>Gi</div><div class="line">/Volumes/hubertinternal  : <span class="number">157</span>Gi</div><div class="line">/Volumes/hubert-tm      : <span class="number">2.3</span>Ti</div></pre></td></tr></table></figure></p>
<h3 id="发送数据到子进程"><a href="#发送数据到子进程" class="headerlink" title="发送数据到子进程"></a>发送数据到子进程</h3><p>前面的两个例子都只使用一个单独的通信通道从第二个进程读取数据。但是通常需要将数据发送到一个命令进行处理。这个例子定义了一个协程来执行Unix命令tr来转换输入流中的字符。在这种情况下，tr用于将小写字母转换为大写字母。to_upper()协程采用事件循环和输入字符串作为参数。它产生了第二个进程运行”tr [:lower:] [:upper:]”。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> asyncio</div><div class="line"><span class="keyword">import</span> asyncio.subprocess</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">def</span> <span class="title">to_upper</span><span class="params">(input)</span>:</span></div><div class="line">    print(<span class="string">'in to_upper'</span>)</div><div class="line"></div><div class="line">    create = asyncio.create_subprocess_exec(</div><div class="line">        <span class="string">'tr'</span>, <span class="string">'[:lower:]'</span>, <span class="string">'[:upper:]'</span>,</div><div class="line">        stdout=asyncio.subprocess.PIPE,</div><div class="line">        stdin=asyncio.subprocess.PIPE,</div><div class="line">    )</div><div class="line">    print(<span class="string">'launching process'</span>)</div><div class="line">    proc = <span class="keyword">await</span> create</div><div class="line">    print(<span class="string">'pid &#123;&#125;'</span>.format(proc.pid))</div></pre></td></tr></table></figure></p>
<p>接下来，to_upper()使用Process的communications()方法将输入字符串发送到命令，并异步读取所有得到的输出。跟ubprocess.Popen版本的方法一样，communications()返回完整的输出字节字符串。如果一个命令可能产生的数据比可以放进内存的数据更多或者输出必须逐步处理，那么输入就不能一次完成，可以直接使用Process的stdin，stdout和stderr句柄，而不用调用communic()。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">print(<span class="string">'communicating with process'</span>)</div><div class="line">stdout, stderr = <span class="keyword">await</span> proc.communicate(input.encode())</div></pre></td></tr></table></figure></p>
<p>I/O完成后，等待进程完全退出，确保正确清理。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">print(<span class="string">'waiting for process to complete'</span>)</div><div class="line"><span class="keyword">await</span> proc.wait()</div></pre></td></tr></table></figure></p>
<p>然后可以检查返回代码，并将输出字节字符串解码，以便从协程中准备返回值。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">return_code = proc.returncode</div><div class="line">print(<span class="string">'return code &#123;&#125;'</span>.format(return_code))</div><div class="line"><span class="keyword">if</span> <span class="keyword">not</span> return_code:</div><div class="line">    results = bytes(stdout).decode()</div><div class="line"><span class="keyword">else</span>:</div><div class="line">    results = <span class="string">''</span></div><div class="line"></div><div class="line"><span class="keyword">return</span> (return_code, results)</div></pre></td></tr></table></figure></p>
<p>程序的主要部分建立一个要转换的消息字符串，然后设置事件循环来运行to_upper()并打印结果。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">MESSAGE = <span class="string">"""</span></div><div class="line"><span class="string">This message will be converted</span></div><div class="line"><span class="string">to all caps.</span></div><div class="line"><span class="string">"""</span></div><div class="line"></div><div class="line">event_loop = asyncio.get_event_loop()</div><div class="line"><span class="keyword">try</span>:</div><div class="line">    return_code, results = event_loop.run_until_complete(</div><div class="line">        to_upper(MESSAGE)</div><div class="line">    )</div><div class="line"><span class="keyword">finally</span>:</div><div class="line">    event_loop.close()</div><div class="line"></div><div class="line"><span class="keyword">if</span> return_code:</div><div class="line">    print(<span class="string">'error exit &#123;&#125;'</span>.format(return_code))</div><div class="line"><span class="keyword">else</span>:</div><div class="line">    print(<span class="string">'Original: &#123;!r&#125;'</span>.format(MESSAGE))</div><div class="line">    print(<span class="string">'Changed : &#123;!r&#125;'</span>.format(results))</div></pre></td></tr></table></figure></p>
<p>输出显示操作的顺序，然后显示如何转换简单的文本消息。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">$ python3 asyncio_subprocess_coroutine_write.py</div><div class="line"></div><div class="line"><span class="keyword">in</span> to_upper</div><div class="line">launching process</div><div class="line">pid <span class="number">49684</span></div><div class="line">communicating <span class="keyword">with</span> process</div><div class="line">waiting <span class="keyword">for</span> process to complete</div><div class="line"><span class="keyword">return</span> code <span class="number">0</span></div><div class="line">Original: <span class="string">'\nThis message will be converted\nto all caps.\n'</span></div><div class="line">Changed : <span class="string">'\nTHIS MESSAGE WILL BE CONVERTED\nTO ALL CAPS.\n'</span></div></pre></td></tr></table></figure></p>
<p><strong>本文翻译自《The Python3 Standard Library By Example》asyncio相关章节</strong></p>
</the></excerpt>
      
    </div>
    
  </div>
  
    
    <div class="copyright">
        <p><span>本文标题:</span><a href="/2017/12/21/asyncio之使用子进程/">asyncio之使用子进程</a></p>
        <p><span>文章作者:</span><a href="/" title="回到主页">HatBoy</a></p>
        <p><span>发布时间:</span>2017-12-21, 18:41:49</p>
        <p><span>最后更新:</span>2017-12-21, 18:43:18</p>
        <p>
            <span>原始链接:</span><a class="post-url" href="/2017/12/21/asyncio之使用子进程/" title="asyncio之使用子进程">http://hatboy.gitee.io/2017/12/21/asyncio之使用子进程/</a>
            <span class="copy-path" data-clipboard-text="原文: http://hatboy.gitee.io/2017/12/21/asyncio之使用子进程/　　作者: HatBoy" title="点击复制文章链接"><i class="fa fa-clipboard"></i></span>
            <script> var clipboard = new Clipboard('.copy-path'); </script>
        </p>
        <p>
            <span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" title="CC BY-NC-SA 4.0 International" target = "_blank">"署名-非商用-相同方式共享 4.0"</a> 转载请保留原文链接及作者。
        </p>
    </div>



    <nav id="article-nav">
        
            <div id="article-nav-newer" class="article-nav-title">
                <a href="/2017/12/21/asyncio之接收Unix信号/">
                    asyncio之接收Unix信号
                </a>
            </div>
        
        
            <div id="article-nav-older" class="article-nav-title">
                <a href="/2017/12/21/asyncio之与DNS服务进行交互/">
                    asyncio之与DNS服务进行交互
                </a>
            </div>
        
    </nav>

  
</article>

    <div id="toc" class="toc-article">
        <strong class="toc-title">文章目录</strong>
        
            <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#使用协议抽象子进程"><span class="toc-number">1.</span> <span class="toc-text">使用协议抽象子进程</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#使用协程和流子进程"><span class="toc-number">2.</span> <span class="toc-text">使用协程和流子进程</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#发送数据到子进程"><span class="toc-number">3.</span> <span class="toc-text">发送数据到子进程</span></a></li></ol>
        
    </div>
    <style>
        .left-col .switch-btn,
        .left-col .switch-area {
            display: none;
        }
        .toc-level-3 i,
        .toc-level-3 ol {
            display: none !important;
        }
    </style>

    <input type="button" id="tocButton" value="隐藏目录"  title="点击按钮隐藏或者显示文章目录">

    <script>
        yiliaConfig.toc = ["隐藏目录", "显示目录", !!"false"];
    </script>



    
<div class="share">
    
        <div class="bdsharebuttonbox">
            <a href="#" class="fa fa-twitter bds_twi" data-cmd="twi" title="分享到推特"></a>
            <a href="#" class="fa fa-weibo bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
            <a href="#" class="fa fa-qq bds_sqq" data-cmd="sqq" title="分享给 QQ 好友"></a>
            <a href="#" class="fa fa-files-o bds_copy" data-cmd="copy" title="复制网址"></a>
            <a href="#" class="fa fa fa-envelope-o bds_mail" data-cmd="mail" title="通过邮件分享"></a>
            <a href="#" class="fa fa-weixin bds_weixin" data-cmd="weixin" title="生成文章二维码"></a>
            <a href="#" class="fa fa-share-alt bds_more" data-cmd="more"></i></a>
        </div>
        <script>
            window._bd_share_config={
                "common":{"bdSnsKey":{},"bdText":"asyncio之使用子进程　| HatBoy的个人主页　","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
        </script>
    

    
</div>







    




    <div class="scroll" id="post-nav-button">
        
            <a href="/2017/12/21/asyncio之接收Unix信号/" title="上一篇: asyncio之接收Unix信号">
                <i class="fa fa-angle-left"></i>
            </a>
        

        <a title="文章列表"><i class="fa fa-bars"></i><i class="fa fa-times"></i></a>

        
            <a href="/2017/12/21/asyncio之与DNS服务进行交互/" title="下一篇: asyncio之与DNS服务进行交互">
                <i class="fa fa-angle-right"></i>
            </a>
        
    </div>

    <ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/2018/02/27/如何恢复丢失的Python源代码如果它还在内存中运行/">如何恢复丢失的Python源代码如果它还在内存中运行</a></li><li class="post-list-item"><a class="post-list-link" href="/2018/02/08/pathlib：文件系统路径作为对象/">pathlib：文件系统路径作为对象</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio：异步IO、事件循环和并发/">asyncio：异步IO、事件循环和并发</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之调试asyncio/">asyncio之调试asyncio</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之协程结合线程和进程/">asyncio之协程结合线程和进程</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之接收Unix信号/">asyncio之接收Unix信号</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之使用子进程/">asyncio之使用子进程</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之与DNS服务进行交互/">asyncio之与DNS服务进行交互</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之使用SSL/">asyncio之使用SSL</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之使用协程和流的异步I-O/">asyncio之使用协程和流的异步I/O</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之协议抽象类的异步I-O/">asyncio之协议抽象类的异步I/O</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之同步原语/">asyncio之同步原语</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之用控制结构组建协程/">asyncio之用控制结构组建协程</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之并发执行Tasks/">asyncio之并发执行Tasks</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之异步生产结果/">asyncio之异步生产结果</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之定时调度常规函数/">asyncio之定时调度常规函数</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之使用协程处理多任务/">asyncio之使用协程处理多任务</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/asyncio之异步并发的概念/">asyncio之异步并发的概念</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/Python上下文管理器/">Python上下文管理器</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/Python-importlib讲解/">Python importlib讲解</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/12/21/第七章-用Cython包装C库/">第七章 用Cython包装C库</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/08/16/第六章-组织Cython代码/">第六章 组织Cython代码</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/08/10/第五章-Cython和扩展类型/">第五章 Cython和扩展类型</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/07/31/第四章-Cython实践：N-body模拟/">第四章 Cython实践：N-body模拟</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/07/31/Python性能分析与调试/">Python性能分析与调试</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/07/17/第三章-深入Cython/">第三章 深入Cython</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/07/03/第二章-编译和运行Cython代码/">第二章 编译和运行Cython代码</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/07/02/第一章-Cython简介/">第一章 Cython简介</a></li></ul>




    <script>
        
    </script>
</div>
      <footer id="footer">
    <div class="outer">
        <div id="footer-info">
            <div class="footer-left">
                <i class="fa fa-copyright"></i> 
                2017-2018 HatBoy
            </div>
            <div class="footer-right">
                <a href="http://hexo.io/" target="_blank" title="快速、简洁且高效的博客框架">Hexo</a>  Theme <a href="https://github.com/MOxFIVE/hexo-theme-yelee" target="_blank" title="简而不减 Hexo 双栏博客主题  v3.5">Yelee</a> by MOxFIVE <i class="fa fa-heart animated infinite pulse"></i>
            </div>
        </div>
        
            <div class="visit">
                
                    <span id="busuanzi_container_site_pv" style='display:none'>
                        <span id="site-visit" title="本站到访数"><i class="fa fa-user" aria-hidden="true"></i><span id="busuanzi_value_site_uv"></span>
                        </span>
                    </span>
                
                
                    <span>| </span>
                
                
                    <span id="busuanzi_container_page_pv" style='display:none'>
                        <span id="page-visit"  title="本页阅读量"><i class="fa fa-eye animated infinite pulse" aria-hidden="true"></i><span id="busuanzi_value_page_pv"></span>
                        </span>
                    </span>
                
            </div>
        
    </div>
</footer>
    </div>
    
<script data-main="/js/main.js" src="//cdn.bootcss.com/require.js/2.2.0/require.min.js"></script>





    <script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    }
});

MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';                 
    }       
});
</script>

<script src="//cdn.bootcss.com/mathjax/2.6.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


<div class="scroll" id="scroll">
    <a href="#" title="返回顶部"><i class="fa fa-arrow-up"></i></a>
    <a href="#comments" onclick="load$hide();" title="查看评论"><i class="fa fa-comments-o"></i></a>
    <a href="#footer" title="转到底部"><i class="fa fa-arrow-down"></i></a>
</div>
<script>
    // Open in New Window
    
        $("a").attr("target", "_blank");
        $("#scroll a, #toc a").removeAttr("target");
    
</script>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
  </div>
</body>
</html>